Read in the data for Final Project

raw_data = read.table("nba_scores.csv",sep = ",",header = TRUE)
elo_initials = read.table("nba_initial_elos.csv",sep = ",",header = TRUE)

HFA & Weight Calclulation for 21st century

year_list = c(2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020)
# list for saving hfa and weight in different years
hfa_list = c()
weight_list = c()
for (x in year_list){
  home_wins = 0
  games = 0
  first_game_index = 1
  scores = raw_data[which(raw_data$season==x),] 
  # Iterate through games - first index can be changed to eliminate early seasons where scores are extreme
  for(i in first_game_index:nrow(scores)) {  
    # Count number of games that do not end in ties
    if(scores$home_score[i] != scores$away_score[i]) { games = games + 1 }
    
    # Count number of games where home team wins
    if(scores$home_score[i] > scores$away_score[i]) { home_wins = home_wins + 1 }
  }
  
  home_win_prob = home_wins / games  # Calculate home win probability where outcome was not a tie
  hfa = -400*log10(1/home_win_prob - 1)  # Calculate number of Elo points added to home team
  hfa_list <- append(hfa_list,hfa)
  
  starting_weight = 0  # Lower bound for weight ranges to be tested - generally set equal to 0
  iterations = 100  # Number of k values to test
  step_size = 0.1  # Amount to increment k by at each step
  first_game_index = 1
  
  # Initialize data frame to store k values and corresponding error
  errors = data.frame(matrix(ncol = 2, nrow = iterations))
  colnames(errors) = c("weight", "error")
  errors$weight = starting_weight + (1:iterations)*step_size
  errors$error = NA
  
  # Iterate through all potential k values that are being tested
  for(counter in 1:iterations) {
    weight = starting_weight + counter*step_size  # Calculate k value for current iteration
    error = 0  # Reset error for current iteration
    elos = read.table("nba_initial_elos.csv", header=TRUE, sep=",")  # Reset initial Elo ratings
    
    # Iterate through games - first index can be changed to eliminate early seasons in a league where early results tend to be extreme
    for(i in first_game_index:nrow(scores)) {  
      # Find indices corresponding to home and away teams for current game
      home_index = which(elos$team == scores$home_team[i])
      away_index = which(elos$team == scores$away_team[i])
      
      # Find home and away team Elo ratings
      home_elo = elos$rating[home_index]
      away_elo = elos$rating[away_index]
  
      # Calculate home team win probability
      win_prob = 1 / (10^((away_elo - (home_elo + hfa*scores$neutral[i]))/400) + 1)
      
      # Calculate actual margin of victory - must be positive
      score_diff = abs(scores$home_score[i] - scores$away_score[i])  
    
      # Determine home team result
      if(scores$home_score[i] > scores$away_score[i]) { 
        home_result = 1  # Home team wins
      } else if(scores$home_score[i] < scores$away_score[i]) { 
        home_result = 0  # Home team loses
      } else { 
        home_result = 0.5  # Tie
      }
      
      # Add squared error between home result and predicted probability of home team winning to SSE
      error = error + (home_result - win_prob)^2
      
      # Calculate amount each team's Elo rating is adjusted by
      home_elo_adjustment = weight * log(score_diff + 1) * (home_result - win_prob)
    
      # Adjust Elo ratings - add point to winner and subtract points from loser
      elos$rating[home_index] = elos$rating[home_index] + home_elo_adjustment
      elos$rating[away_index] = elos$rating[away_index] - home_elo_adjustment
  
      # Adjust Elo ratings at end of season to regress 1/3 of the way towards 1500
      if(i < nrow(scores) && scores$season[i+1] > scores$season[i]) {
        for(j in 1:nrow(elos)) {
          if(scores$season[i] >= elos$inaugural_season[j]) {
            elos$rating[j] = elos$rating[j] - (elos$rating[j] - 1500)/3
          }
        }
      
        existing_teams = elos[which(elos$inaugural_season <= (scores$season[i] + 1)),]
        expansion_adjustment = -1*(mean(existing_teams$rating) - 1500)
      
        for(j in 1:nrow(elos)) {
          if((scores$season[i] + 1) >= elos$inaugural_season[j]) {
            elos$rating[j] = elos$rating[j] + expansion_adjustment
          }
        }
      }
    }
    errors$error[counter] = error  # Store error for current iteration
  }
  
  # Choose and print optimal weight based on value that had the lowest SSE
  weight = errors$weight[which(errors$error == min(errors$error))]
  weight_list <- append(weight_list,weight)
}

print(hfa_list)
##  [1] 68.14165 64.13962 90.41023 85.61199 71.86992 76.27353 66.90464 77.83927
##  [9] 79.29028 69.77472 76.19808 65.46320 80.18853 55.52015 53.14886 66.26620
## [17] 58.13276 60.99658 63.73097 32.62010 32.43418
print(weight_list)
##  [1] 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0 10.0
## [16] 10.0  9.9 10.0 10.0 10.0 10.0

SAVE weight and hfa into csv fie(The calculation takes long time so save it into csv file for later use)

write.table(as.data.frame(hfa_list),file="hfa.csv", quote=F,sep=",",row.names=F)
write.table(as.data.frame(weight_list),file="weight.csv", quote=F,sep=",",row.names=F)

Read HFA and wight data from csv;

## Read HFA and wight data from csv
hfa_list=read.csv("hfa.csv")
weight_list=read.csv("weight.csv")

Set up

#Only use data from 2000 to 2020
scores = raw_data[which(raw_data$season > 1999),]
#Get all the team name
team_name=unique(scores$home_team)
#Combine hfa and weight into dataframe
Adjustment<-cbind(hfa_list,weight_list)

Elo Ratings Calclulation for 21st century

# Use list to store average elo ratings for each year from 2000 to 2020
stack=list()
for(teamN in team_name){
  
inital_year=2000

# Optimal weight from code above. If the above code is run first, the line below can be commented out. Otherwise, you can type the optimal k-value below without needing to run the chunk of code above.
for(i in 1:nrow(Adjustment)){

weight = Adjustment[i,"weight_list"]
hfa =Adjustment[i,"hfa_list"]

# Select team and season to follow for a period of time
team = teamN
first_season = inital_year
last_season = inital_year

inital_year=inital_year+1

# Read in initial team Elo ratings and history of games
elos = read.table("nba_initial_elos.csv", header=TRUE, sep=",")
scores =raw_data[which(raw_data$season<=first_season),] 

# Create data frame to store information for team specified above
team_results = data.frame(matrix(ncol = 8, nrow = 0))
colnames(team_results) = c("opponent", "pregame_elo", "win_probability", "result", "team_score", "opponent_score", "elo_adjustment", "postgame_elo")

# Iterate through all games in the sport's history
for(i in 1:nrow(scores)) {
  # Find indices corresponding to home and away teams for current game
  home_index = which(elos$team == scores$home_team[i])
  away_index = which(elos$team == scores$away_team[i])
  
  # Find home and away team Elo ratings
  home_elo = elos$rating[home_index]
  away_elo = elos$rating[away_index]

  # Calculate home team win probability
  win_prob = 1 / (10^((away_elo - (home_elo + hfa*scores$neutral[i]))/400) + 1)

  # Calculate actual margin of victory - must be positive
  score_diff = abs(scores$home_score[i] - scores$away_score[i])   
  
  # Determine home team result
  if(scores$home_score[i] > scores$away_score[i]) { 
    home_result = 1  # Home team wins
  } else if(scores$home_score[i] < scores$away_score[i]) { 
    home_result = 0  # Home team loses
  } else { 
    home_result = 0.5  # Tie
  }
  
  # Calculate amount each team's Elo rating is adjusted by
  home_elo_adjustment = weight * log(score_diff + 1) * (home_result - win_prob)
  
  # Adjust Elo ratings - add point to winner and subtract points from loser
  elos$rating[home_index] = elos$rating[home_index] + home_elo_adjustment
  elos$rating[away_index] = elos$rating[away_index] - home_elo_adjustment
  
  # Add game information to team result data frame for each team game of the team specified above if team and season both match
  if(scores$season[i] >= first_season & scores$season[i] <= last_season & (scores$home_team[i] == team | scores$away_team[i] == team)) {
    if(scores$home_team[i] == team) {  # If specified team was at home
      team_results[nrow(team_results) + 1,] = c(scores$away_team[i], elos$rating[home_index] - home_elo_adjustment, win_prob, home_result, scores$home_score[i], scores$away_score[i], home_elo_adjustment, elos$rating[home_index])
    } else {  # If specified team was away
      team_results[nrow(team_results) + 1,] = c(scores$home_team[i], elos$rating[away_index] + home_elo_adjustment, 1-win_prob, 1-home_result, scores$away_score[i], scores$home_score[i], -1*home_elo_adjustment, elos$rating[away_index])
    }
  }
  
  # Adjust Elo ratings at end of season to regress 1/3 of the way towards 1500
  if(i < nrow(scores) && scores$season[i+1] > scores$season[i]) {  # New season
    for(j in 1:nrow(elos)) {  # For each team
      if(scores$season[i] >= elos$inaugural_season[j]) {  # Check if team existed
        # Move each team's Elo rating back towards 1500 by 1/3 of the difference
        elos$rating[j] = elos$rating[j] - (elos$rating[j] - 1500)/3
      }
    }
    
    # Identify all teams that existed at beginning of following season
    existing_teams = elos[which(elos$inaugural_season <= (scores$season[i] + 1)),]
    
    # Calculate amount each team's Elo rating must be adjusted by to make mean 1500
    expansion_adjustment = -1*(mean(existing_teams$rating) - 1500)
    
    # Perform expansion adjustment on teams that existed at beginning of following season
    for(j in 1:nrow(elos)) {  # For each team
      if((scores$season[i] + 1) >= elos$inaugural_season[j]) {  # Check if team existed
        elos$rating[j] = elos$rating[j] + expansion_adjustment  # Update ratings if so
      }
    }
  }
}
# Change data type to numeric
temp=transform(team_results,postgame_elo=as.numeric(postgame_elo))
# Store the elo into the list
stack=append(stack,mean(temp$postgame_elo))
}
}

Clean elo rating for each team and store everything in to df

# Create a dataframe that contains teams' name and their elo rating for each year
df=data.frame(matrix(ncol=30,nrow=21))
colnames(df)=team_name
team_number=1
row=1
for(i in 1:length(stack)){
  df[row,team_number]=stack[i]
  row=row+1
  if(i%%21==0){
    team_number=team_number+1
    row=1
  }
 
}
## Remove nan value in the elo rating
## Since these nan values are caused by no games or team reorganize so we initial it as the initial elo ratings
df[1,30]=1400
df[2,30]=1400
df[3,"Charlotte Hornets"]=1400
df[4,"Charlotte Hornets"]=1400

print(df)
##    Orlando Magic Atlanta Hawks Brooklyn Nets Toronto Raptors New York Knicks
## 1       1512.578      1348.977      1382.767        1529.481        1552.360
## 2       1519.962      1385.598      1537.059        1520.403        1438.110
## 3       1502.088      1414.345      1601.256        1383.043        1430.943
## 4       1362.400      1388.205      1578.669        1396.073        1455.673
## 5       1420.943      1287.624      1465.098        1420.775        1435.460
## 6       1408.274      1297.400      1550.400        1395.466        1353.064
## 7       1487.848      1379.312      1504.171        1480.783        1393.329
## 8       1574.085      1435.444      1438.920        1530.162        1349.833
## 9       1660.072      1531.681      1434.610        1441.936        1393.674
## 10      1671.369      1592.194      1282.836        1470.645        1388.507
## 11      1642.035      1547.372      1329.773        1366.798        1476.039
## 12      1567.552      1540.793      1332.073        1357.368        1492.788
## 13      1361.786      1530.214      1493.860        1413.401        1593.159
## 14      1320.602      1477.171      1502.553        1540.488        1471.181
## 15      1330.203      1625.417      1461.463        1573.115        1342.797
## 16      1420.435      1566.538      1359.656        1593.502        1386.316
## 17      1394.437      1512.875      1294.377        1594.329        1415.081
## 18      1369.359      1379.129      1373.786        1644.333        1414.033
## 19      1444.077      1348.934      1452.529        1655.317        1307.248
## 20      1486.428      1355.032      1473.297        1679.725        1311.988
## 21      1392.213      1505.946      1578.862        1528.453        1483.369
##    Dallas Mavericks Houston Rockets San Antonio Spurs Chicago Bulls Utah Jazz
## 1          1591.778        1537.814          1675.189      1248.654  1619.731
## 2          1635.241        1456.316          1671.600      1281.701  1558.476
## 3          1704.404        1504.038          1681.439      1352.033  1560.562
## 4          1621.615        1561.089          1695.795      1346.120  1505.034
## 5          1638.867        1567.068          1722.777      1461.916  1425.894
## 6          1692.314        1505.854          1702.870      1498.784  1440.638
## 7          1714.482        1573.407          1687.105      1568.286  1581.448
## 8          1628.145        1606.177          1668.317      1473.430  1623.492
## 9          1570.834        1621.814          1624.047      1471.621  1596.866
## 10         1603.383        1560.179          1595.411      1488.580  1602.854
## 11         1650.893        1505.318          1672.686      1629.686  1532.371
## 12         1605.043        1520.907          1662.311      1675.369  1493.342
## 13         1499.577        1545.655          1700.623      1560.450  1519.892
## 14         1569.809        1613.511          1703.097      1528.041  1411.149
## 15         1598.562        1634.076          1675.839      1568.128  1429.687
## 16         1530.417        1533.151          1748.373      1511.650  1515.781
## 17         1441.790        1605.544          1707.823      1483.867  1578.967
## 18         1409.193        1698.182          1588.077      1406.540  1568.108
## 19         1431.524        1630.326          1548.938      1318.918  1596.887
## 20         1550.709        1618.926          1495.799      1359.948  1593.988
## 21         1544.804        1397.140          1502.575      1426.581  1646.461
##    Portland Trailblazers Golden State Warriors Memphis Grizzlies
## 1               1642.740              1312.091          1362.745
## 2               1570.541              1320.778          1329.445
## 3               1599.260              1428.947          1372.699
## 4               1514.750              1468.902          1537.364
## 5               1441.475              1422.575          1556.248
## 6               1345.612              1473.983          1560.822
## 7               1374.908              1486.709          1406.928
## 8               1475.123              1581.835          1355.412
## 9               1566.178              1443.175          1322.824
## 10              1583.983              1381.520          1458.657
## 11              1552.494              1428.487          1527.733
## 12              1523.934              1451.575          1579.043
## 13              1450.839              1514.690          1633.213
## 14              1578.012              1599.087          1576.986
## 15              1617.572              1729.056          1631.497
## 16              1536.882              1819.362          1532.061
## 17              1522.617              1776.272          1513.912
## 18              1560.228              1720.183          1402.190
## 19              1607.011              1678.043          1417.907
## 20              1511.423              1420.175          1451.602
## 21              1525.967              1463.331          1527.203
##    Philadelphia 76ers Boston Celtics Cleveland Cavaliers Charlotte Hornets
## 1            1626.664       1445.540            1413.022          1579.744
## 2            1544.190       1534.978            1384.473          1534.144
## 3            1552.466       1532.924            1296.688          1400.000
## 4            1465.920       1446.751            1373.631          1400.000
## 5            1456.548       1491.084            1500.606          1369.137
## 6            1469.158       1466.464            1547.565          1346.137
## 7            1418.231       1377.199            1575.397          1398.292
## 8            1477.296       1653.579            1539.650          1412.513
## 9            1500.772       1710.315            1696.196          1438.418
## 10           1416.064       1628.131            1725.939          1509.773
## 11           1481.746       1663.776            1376.380          1450.106
## 12           1547.014       1577.060            1361.565          1276.994
## 13           1447.657       1529.151            1339.011          1244.856
## 14           1306.377       1404.732            1375.072          1428.789
## 15           1266.877       1414.549            1568.167          1449.779
## 16           1231.525       1552.119            1672.535          1504.589
## 17           1320.339       1583.494            1645.776          1511.207
## 18           1524.922       1609.203            1580.703          1465.218
## 19           1597.386       1589.661            1363.455          1481.095
## 20           1588.602       1643.199            1322.577          1401.040
## 21           1597.592       1555.697            1360.845          1452.479
##    Miami Heat Oklahoma City Thunder Los Angeles Lakers Denver Nuggets
## 1    1580.383              1516.280           1660.021       1466.015
## 2    1451.793              1565.449           1712.430       1401.871
## 3    1403.929              1516.953           1612.539       1316.763
## 4    1446.529              1490.728           1640.088       1473.772
## 5    1642.723              1591.700           1522.708       1526.876
## 6    1622.909              1460.803           1511.214       1537.941
## 7    1532.870              1450.857           1537.886       1520.907
## 8    1333.792              1338.357           1638.245       1573.056
## 9    1447.051              1306.026           1739.274       1611.590
## 10   1521.518              1530.456           1691.358       1628.885
## 11   1660.722              1607.454           1663.049       1587.122
## 12   1683.304              1685.590           1596.489       1574.166
## 13   1715.831              1701.643           1531.048       1622.817
## 14   1672.894              1687.167           1413.574       1511.743
## 15   1495.546              1578.792           1353.524       1438.651
## 16   1531.055              1645.570           1281.701       1423.737
## 17   1499.393              1594.343           1351.878       1471.559
## 18   1521.597              1553.924           1419.346       1534.241
## 19   1478.437              1586.461           1481.179       1601.815
## 20   1580.975              1571.687           1636.367       1598.682
## 21   1537.013              1425.462           1619.727       1602.220
##    Phoenix Suns Los Angeles Clippers Washington Wizards Detroit Pistons
## 1      1595.597             1346.182           1342.800        1442.917
## 2      1519.109             1498.220           1421.632        1527.419
## 3      1522.861             1414.939           1459.551        1588.445
## 4      1433.860             1414.790           1367.160        1620.196
## 5      1641.622             1451.457           1474.865        1627.845
## 6      1657.980             1544.845           1496.490        1713.694
## 7      1674.107             1509.988           1519.611        1628.539
## 8      1647.815             1411.232           1482.104        1660.406
## 9      1561.894             1312.553           1348.677        1539.739
## 10     1598.623             1363.242           1353.936        1387.911
## 11     1544.909             1377.159           1320.537        1379.259
## 12     1504.812             1536.857           1309.898        1377.531
## 13     1422.806             1629.449           1383.476        1396.885
## 14     1515.269             1648.869           1501.079        1397.924
## 15     1544.841             1653.669           1543.050        1387.393
## 16     1382.318             1625.293           1493.885        1498.609
## 17     1343.418             1607.481           1545.416        1483.242
## 18     1324.254             1535.641           1541.475        1471.678
## 19     1280.644             1537.639           1447.112        1483.617
## 20     1412.429             1618.544           1396.223        1409.682
## 21     1628.820             1632.801           1412.243        1361.860
##    Indiana Pacers Minnesota Timberwolves Milwaukee Bucks Sacramento Kings
## 1        1525.005               1559.074        1573.391         1627.307
## 2        1496.920               1601.366        1555.892         1677.376
## 3        1552.680               1568.133        1482.015         1670.818
## 4        1622.613               1644.186        1503.928         1659.236
## 5        1551.911               1558.584        1435.059         1595.298
## 6        1535.426               1498.267        1444.227         1514.539
## 7        1469.696               1447.527        1421.174         1482.851
## 8        1429.621               1319.242        1368.186         1450.946
## 9        1451.712               1351.452        1414.023         1346.882
## 10       1415.962               1291.827        1497.597         1341.896
## 11       1467.955               1296.799        1477.586         1319.888
## 12       1550.653               1411.930        1480.390         1387.802
## 13       1584.230               1421.391        1475.542         1377.870
## 14       1623.789               1523.177        1301.804         1398.588
## 15       1476.973               1348.713        1437.570         1411.745
## 16       1532.926               1339.507        1424.681         1425.552
## 17       1502.295               1430.135        1470.314         1415.515
## 18       1529.064               1530.215        1509.227         1348.429
## 19       1577.161               1500.547        1661.946         1456.291
## 20       1554.705               1413.028        1713.567         1445.720
## 21       1502.121               1334.271        1622.999         1440.451
##    New Orleans Pelicans
## 1              1400.000
## 2              1400.000
## 3              1489.062
## 4              1509.359
## 5              1356.579
## 6              1441.606
## 7              1459.113
## 8              1604.971
## 9              1596.136
## 10             1476.641
## 11             1526.021
## 12             1408.431
## 13             1407.933
## 14             1431.714
## 15             1495.990
## 16             1448.355
## 17             1417.812
## 18             1528.619
## 19             1511.747
## 20             1454.883
## 21             1486.189
df
##    Orlando Magic Atlanta Hawks Brooklyn Nets Toronto Raptors New York Knicks
## 1       1512.578      1348.977      1382.767        1529.481        1552.360
## 2       1519.962      1385.598      1537.059        1520.403        1438.110
## 3       1502.088      1414.345      1601.256        1383.043        1430.943
## 4       1362.400      1388.205      1578.669        1396.073        1455.673
## 5       1420.943      1287.624      1465.098        1420.775        1435.460
## 6       1408.274      1297.400      1550.400        1395.466        1353.064
## 7       1487.848      1379.312      1504.171        1480.783        1393.329
## 8       1574.085      1435.444      1438.920        1530.162        1349.833
## 9       1660.072      1531.681      1434.610        1441.936        1393.674
## 10      1671.369      1592.194      1282.836        1470.645        1388.507
## 11      1642.035      1547.372      1329.773        1366.798        1476.039
## 12      1567.552      1540.793      1332.073        1357.368        1492.788
## 13      1361.786      1530.214      1493.860        1413.401        1593.159
## 14      1320.602      1477.171      1502.553        1540.488        1471.181
## 15      1330.203      1625.417      1461.463        1573.115        1342.797
## 16      1420.435      1566.538      1359.656        1593.502        1386.316
## 17      1394.437      1512.875      1294.377        1594.329        1415.081
## 18      1369.359      1379.129      1373.786        1644.333        1414.033
## 19      1444.077      1348.934      1452.529        1655.317        1307.248
## 20      1486.428      1355.032      1473.297        1679.725        1311.988
## 21      1392.213      1505.946      1578.862        1528.453        1483.369
##    Dallas Mavericks Houston Rockets San Antonio Spurs Chicago Bulls Utah Jazz
## 1          1591.778        1537.814          1675.189      1248.654  1619.731
## 2          1635.241        1456.316          1671.600      1281.701  1558.476
## 3          1704.404        1504.038          1681.439      1352.033  1560.562
## 4          1621.615        1561.089          1695.795      1346.120  1505.034
## 5          1638.867        1567.068          1722.777      1461.916  1425.894
## 6          1692.314        1505.854          1702.870      1498.784  1440.638
## 7          1714.482        1573.407          1687.105      1568.286  1581.448
## 8          1628.145        1606.177          1668.317      1473.430  1623.492
## 9          1570.834        1621.814          1624.047      1471.621  1596.866
## 10         1603.383        1560.179          1595.411      1488.580  1602.854
## 11         1650.893        1505.318          1672.686      1629.686  1532.371
## 12         1605.043        1520.907          1662.311      1675.369  1493.342
## 13         1499.577        1545.655          1700.623      1560.450  1519.892
## 14         1569.809        1613.511          1703.097      1528.041  1411.149
## 15         1598.562        1634.076          1675.839      1568.128  1429.687
## 16         1530.417        1533.151          1748.373      1511.650  1515.781
## 17         1441.790        1605.544          1707.823      1483.867  1578.967
## 18         1409.193        1698.182          1588.077      1406.540  1568.108
## 19         1431.524        1630.326          1548.938      1318.918  1596.887
## 20         1550.709        1618.926          1495.799      1359.948  1593.988
## 21         1544.804        1397.140          1502.575      1426.581  1646.461
##    Portland Trailblazers Golden State Warriors Memphis Grizzlies
## 1               1642.740              1312.091          1362.745
## 2               1570.541              1320.778          1329.445
## 3               1599.260              1428.947          1372.699
## 4               1514.750              1468.902          1537.364
## 5               1441.475              1422.575          1556.248
## 6               1345.612              1473.983          1560.822
## 7               1374.908              1486.709          1406.928
## 8               1475.123              1581.835          1355.412
## 9               1566.178              1443.175          1322.824
## 10              1583.983              1381.520          1458.657
## 11              1552.494              1428.487          1527.733
## 12              1523.934              1451.575          1579.043
## 13              1450.839              1514.690          1633.213
## 14              1578.012              1599.087          1576.986
## 15              1617.572              1729.056          1631.497
## 16              1536.882              1819.362          1532.061
## 17              1522.617              1776.272          1513.912
## 18              1560.228              1720.183          1402.190
## 19              1607.011              1678.043          1417.907
## 20              1511.423              1420.175          1451.602
## 21              1525.967              1463.331          1527.203
##    Philadelphia 76ers Boston Celtics Cleveland Cavaliers Charlotte Hornets
## 1            1626.664       1445.540            1413.022          1579.744
## 2            1544.190       1534.978            1384.473          1534.144
## 3            1552.466       1532.924            1296.688          1400.000
## 4            1465.920       1446.751            1373.631          1400.000
## 5            1456.548       1491.084            1500.606          1369.137
## 6            1469.158       1466.464            1547.565          1346.137
## 7            1418.231       1377.199            1575.397          1398.292
## 8            1477.296       1653.579            1539.650          1412.513
## 9            1500.772       1710.315            1696.196          1438.418
## 10           1416.064       1628.131            1725.939          1509.773
## 11           1481.746       1663.776            1376.380          1450.106
## 12           1547.014       1577.060            1361.565          1276.994
## 13           1447.657       1529.151            1339.011          1244.856
## 14           1306.377       1404.732            1375.072          1428.789
## 15           1266.877       1414.549            1568.167          1449.779
## 16           1231.525       1552.119            1672.535          1504.589
## 17           1320.339       1583.494            1645.776          1511.207
## 18           1524.922       1609.203            1580.703          1465.218
## 19           1597.386       1589.661            1363.455          1481.095
## 20           1588.602       1643.199            1322.577          1401.040
## 21           1597.592       1555.697            1360.845          1452.479
##    Miami Heat Oklahoma City Thunder Los Angeles Lakers Denver Nuggets
## 1    1580.383              1516.280           1660.021       1466.015
## 2    1451.793              1565.449           1712.430       1401.871
## 3    1403.929              1516.953           1612.539       1316.763
## 4    1446.529              1490.728           1640.088       1473.772
## 5    1642.723              1591.700           1522.708       1526.876
## 6    1622.909              1460.803           1511.214       1537.941
## 7    1532.870              1450.857           1537.886       1520.907
## 8    1333.792              1338.357           1638.245       1573.056
## 9    1447.051              1306.026           1739.274       1611.590
## 10   1521.518              1530.456           1691.358       1628.885
## 11   1660.722              1607.454           1663.049       1587.122
## 12   1683.304              1685.590           1596.489       1574.166
## 13   1715.831              1701.643           1531.048       1622.817
## 14   1672.894              1687.167           1413.574       1511.743
## 15   1495.546              1578.792           1353.524       1438.651
## 16   1531.055              1645.570           1281.701       1423.737
## 17   1499.393              1594.343           1351.878       1471.559
## 18   1521.597              1553.924           1419.346       1534.241
## 19   1478.437              1586.461           1481.179       1601.815
## 20   1580.975              1571.687           1636.367       1598.682
## 21   1537.013              1425.462           1619.727       1602.220
##    Phoenix Suns Los Angeles Clippers Washington Wizards Detroit Pistons
## 1      1595.597             1346.182           1342.800        1442.917
## 2      1519.109             1498.220           1421.632        1527.419
## 3      1522.861             1414.939           1459.551        1588.445
## 4      1433.860             1414.790           1367.160        1620.196
## 5      1641.622             1451.457           1474.865        1627.845
## 6      1657.980             1544.845           1496.490        1713.694
## 7      1674.107             1509.988           1519.611        1628.539
## 8      1647.815             1411.232           1482.104        1660.406
## 9      1561.894             1312.553           1348.677        1539.739
## 10     1598.623             1363.242           1353.936        1387.911
## 11     1544.909             1377.159           1320.537        1379.259
## 12     1504.812             1536.857           1309.898        1377.531
## 13     1422.806             1629.449           1383.476        1396.885
## 14     1515.269             1648.869           1501.079        1397.924
## 15     1544.841             1653.669           1543.050        1387.393
## 16     1382.318             1625.293           1493.885        1498.609
## 17     1343.418             1607.481           1545.416        1483.242
## 18     1324.254             1535.641           1541.475        1471.678
## 19     1280.644             1537.639           1447.112        1483.617
## 20     1412.429             1618.544           1396.223        1409.682
## 21     1628.820             1632.801           1412.243        1361.860
##    Indiana Pacers Minnesota Timberwolves Milwaukee Bucks Sacramento Kings
## 1        1525.005               1559.074        1573.391         1627.307
## 2        1496.920               1601.366        1555.892         1677.376
## 3        1552.680               1568.133        1482.015         1670.818
## 4        1622.613               1644.186        1503.928         1659.236
## 5        1551.911               1558.584        1435.059         1595.298
## 6        1535.426               1498.267        1444.227         1514.539
## 7        1469.696               1447.527        1421.174         1482.851
## 8        1429.621               1319.242        1368.186         1450.946
## 9        1451.712               1351.452        1414.023         1346.882
## 10       1415.962               1291.827        1497.597         1341.896
## 11       1467.955               1296.799        1477.586         1319.888
## 12       1550.653               1411.930        1480.390         1387.802
## 13       1584.230               1421.391        1475.542         1377.870
## 14       1623.789               1523.177        1301.804         1398.588
## 15       1476.973               1348.713        1437.570         1411.745
## 16       1532.926               1339.507        1424.681         1425.552
## 17       1502.295               1430.135        1470.314         1415.515
## 18       1529.064               1530.215        1509.227         1348.429
## 19       1577.161               1500.547        1661.946         1456.291
## 20       1554.705               1413.028        1713.567         1445.720
## 21       1502.121               1334.271        1622.999         1440.451
##    New Orleans Pelicans
## 1              1400.000
## 2              1400.000
## 3              1489.062
## 4              1509.359
## 5              1356.579
## 6              1441.606
## 7              1459.113
## 8              1604.971
## 9              1596.136
## 10             1476.641
## 11             1526.021
## 12             1408.431
## 13             1407.933
## 14             1431.714
## 15             1495.990
## 16             1448.355
## 17             1417.812
## 18             1528.619
## 19             1511.747
## 20             1454.883
## 21             1486.189

Save elo into csv file

write.csv(df,"elo.csv",row.names = FALSE)

import elo data

df=read.csv("elo.csv")

Analysize data store mean, median and sd in the list

# List for storing mean,median,and sd
mean_stack=list()
sd_stack=list()
median_stack=list()

# Calculate mean, median,and sd
for(i in colnames(df)){
  mean_stack=append(mean_stack,mean(df[,i]))
  sd_stack=append(sd_stack,sd(df[,i]))
  median_stack=append(median_stack,median(df[,i]))
}

Get Descriptive Statistics and store them in the descriptive dataframe

# Store everything in the descriptive_stat dataframe
mean_df=as.data.frame(do.call(rbind,mean_stack))
colnames(mean_df)="mean"
sd_df=as.data.frame(do.call(rbind,sd_stack))
colnames(sd_df)="sd"
median_df=as.data.frame(do.call(rbind,median_stack))
colnames(median_df)="median"
descriptive_stat=cbind(colnames(df),mean_df,sd_df,median_df)
colnames(descriptive_stat)=c("team","mean","sd","median")

store the Descriptive Statistics

write.csv(descriptive_stat,"descriptive.csv",row.names = FALSE)

import Descriptive Statistics

descriptive_stat=read.csv("descriptive.csv")

Reset the team name since the names have some changes

team_name=colnames(df)

Create plot of Elo ratings for different team from 2000-2020

library(ggplot2)

df$year=2000:2020
#Create plot of Elo ratings for different team from 2000-2020
for(i in team_name){

 plot.ts(df[,i],xlab = "Year from 2000 to 2020",  ylab = "Elo Rating",main = paste("Team: ",i))
  #axis(1,df$year)
}

Calculate autocrrelation and durbinWatsontest for the linear model

library(car)
## Loading required package: carData
for (i in team_name){
  cat(paste("Team: ",i))
  cat("\n")
  #Construct the linear model
  cur_model=lm(df[,i]~df[,"year"])
  # formating
  print(durbinWatsonTest(cur_model))
  cat("\n")
  cat("\n")
}
## Team:  Orlando.Magic
##  lag Autocorrelation D-W Statistic p-value
##    1       0.7078625      0.581168       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Atlanta.Hawks
##  lag Autocorrelation D-W Statistic p-value
##    1       0.6832184     0.6229795       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Brooklyn.Nets
##  lag Autocorrelation D-W Statistic p-value
##    1       0.4444372     0.8767611   0.002
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Toronto.Raptors
##  lag Autocorrelation D-W Statistic p-value
##    1       0.5082855     0.8085398   0.002
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  New.York.Knicks
##  lag Autocorrelation D-W Statistic p-value
##    1       0.3163093      1.187897   0.024
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Dallas.Mavericks
##  lag Autocorrelation D-W Statistic p-value
##    1       0.3304898      1.163481   0.024
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Houston.Rockets
##  lag Autocorrelation D-W Statistic p-value
##    1       0.1085938      1.329416   0.072
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  San.Antonio.Spurs
##  lag Autocorrelation D-W Statistic p-value
##    1       0.6529772     0.5509981       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Chicago.Bulls
##  lag Autocorrelation D-W Statistic p-value
##    1       0.7297002     0.3983664       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Utah.Jazz
##  lag Autocorrelation D-W Statistic p-value
##    1         0.55544     0.7217189       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Portland.Trailblazers
##  lag Autocorrelation D-W Statistic p-value
##    1       0.5259033     0.8019058   0.002
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Golden.State.Warriors
##  lag Autocorrelation D-W Statistic p-value
##    1       0.6095012     0.6132759       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Memphis.Grizzlies
##  lag Autocorrelation D-W Statistic p-value
##    1       0.6370672     0.7025395   0.002
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Philadelphia.76ers
##  lag Autocorrelation D-W Statistic p-value
##    1        0.654117     0.5185056       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Boston.Celtics
##  lag Autocorrelation D-W Statistic p-value
##    1       0.4408933      1.093947   0.006
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Cleveland.Cavaliers
##  lag Autocorrelation D-W Statistic p-value
##    1       0.5886311     0.7720176   0.004
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Charlotte.Hornets
##  lag Autocorrelation D-W Statistic p-value
##    1       0.4752125     0.8775541       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Miami.Heat
##  lag Autocorrelation D-W Statistic p-value
##    1       0.4750486      1.021841   0.004
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Oklahoma.City.Thunder
##  lag Autocorrelation D-W Statistic p-value
##    1       0.5584059     0.7497431   0.002
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Los.Angeles.Lakers
##  lag Autocorrelation D-W Statistic p-value
##    1       0.7069464     0.4759136       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Denver.Nuggets
##  lag Autocorrelation D-W Statistic p-value
##    1       0.6666633     0.6630547       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Phoenix.Suns
##  lag Autocorrelation D-W Statistic p-value
##    1       0.4045231     0.9464731   0.004
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Los.Angeles.Clippers
##  lag Autocorrelation D-W Statistic p-value
##    1       0.5917471     0.8028749   0.002
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Washington.Wizards
##  lag Autocorrelation D-W Statistic p-value
##    1       0.5851073     0.7680514       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Detroit.Pistons
##  lag Autocorrelation D-W Statistic p-value
##    1       0.6492766     0.5486223       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Indiana.Pacers
##  lag Autocorrelation D-W Statistic p-value
##    1       0.4859199      1.015781   0.002
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Minnesota.Timberwolves
##  lag Autocorrelation D-W Statistic p-value
##    1       0.6335969     0.7228651   0.002
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Milwaukee.Bucks
##  lag Autocorrelation D-W Statistic p-value
##    1       0.5960961     0.6605117       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Sacramento.Kings
##  lag Autocorrelation D-W Statistic p-value
##    1        0.790246     0.3386508       0
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  New.Orleans.Pelicans
##  lag Autocorrelation D-W Statistic p-value
##    1        0.254611      1.455674   0.132
##  Alternative hypothesis: rho != 0

Get optimal order of autoregressive model

library(car)
# Create a list to store the optimal order
opt_order=list()
for(i in team_name){
  # Construct the autoregressive model
  model_time=ar.ols(df[,i],order.max = 6, demean = F, intercept = T)
  # formating
  cat(paste("\nTeam: ",i),"\n")
  cat("\n")
  print(model_time)
  cat("\n")
  cat("\n")
  # store the order into list
 opt_order=append(opt_order,model_time$order)
  
}
## 
## Team:  Orlando.Magic 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2  
##  1.0700  -0.4469  
## 
## Intercept: 548.2 (224) 
## 
## Order selected 2  sigma^2 estimated as  4470
## 
## 
## 
## Team:  Atlanta.Hawks 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5  
##  0.8293  -0.0028  -0.4624  -0.0372   0.2048  
## 
## Intercept: 702.6 (283.4) 
## 
## Order selected 5  sigma^2 estimated as  3078
## 
## 
## 
## Team:  Brooklyn.Nets 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5        6  
##  0.6474  -0.0107  -0.5359  -0.2100   0.4763  -0.2263  
## 
## Intercept: 1221 (384.6) 
## 
## Order selected 6  sigma^2 estimated as  2504
## 
## 
## 
## Team:  Toronto.Raptors 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3  
##  0.8775  -0.0234  -0.1993  
## 
## Intercept: 521.3 (252.3) 
## 
## Order selected 3  sigma^2 estimated as  3439
## 
## 
## 
## Team:  New.York.Knicks 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##      1  
## 0.3608  
## 
## Intercept: 904.2 (273.1) 
## 
## Order selected 1  sigma^2 estimated as  3830
## 
## 
## 
## Team:  Dallas.Mavericks 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5  
##  1.0729  -0.8805   0.4968   0.2662  -0.0231  
## 
## Intercept: 85.29 (319.4) 
## 
## Order selected 5  sigma^2 estimated as  1568
## 
## 
## 
## Team:  Houston.Rockets 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5        6  
##  0.3829  -0.3951  -0.0897  -0.6887   0.7449  -0.7839  
## 
## Intercept: 2875 (742.3) 
## 
## Order selected 6  sigma^2 estimated as  2253
## 
## 
## 
## Team:  San.Antonio.Spurs 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4  
##  0.8795  -0.2906   0.0830  -0.8105  
## 
## Intercept: 1898 (628) 
## 
## Order selected 4  sigma^2 estimated as  1360
## 
## 
## 
## Team:  Chicago.Bulls 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##      1  
## 0.7219  
## 
## Intercept: 415.4 (179) 
## 
## Order selected 1  sigma^2 estimated as  3771
## 
## 
## 
## Team:  Utah.Jazz 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2  
##  0.9136  -0.4152  
## 
## Intercept: 774.3 (299.9) 
## 
## Order selected 2  sigma^2 estimated as  2494
## 
## 
## 
## Team:  Portland.Trailblazers 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5        6  
##  0.6971  -0.4423   0.1689  -0.1047   0.0678  -0.0530  
## 
## Intercept: 1024 (506.4) 
## 
## Order selected 6  sigma^2 estimated as  1957
## 
## 
## 
## Team:  Golden.State.Warriors 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##      1  
## 0.7605  
## 
## Intercept: 372.3 (194.6) 
## 
## Order selected 1  sigma^2 estimated as  6935
## 
## 
## 
## Team:  Memphis.Grizzlies 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5        6  
##  0.9139   0.4658  -1.3497   0.2278   0.6656  -0.4960  
## 
## Intercept: 860.1 (314.2) 
## 
## Order selected 6  sigma^2 estimated as  1346
## 
## 
## 
## Team:  Philadelphia.76ers 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5  
##  1.2578  -1.1063   0.8756  -1.1389   0.4537  
## 
## Intercept: 942.5 (312.3) 
## 
## Order selected 5  sigma^2 estimated as  1887
## 
## 
## 
## Team:  Boston.Celtics 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4  
##  0.5609  -0.2759   0.1522  -0.4906  
## 
## Intercept: 1629 (414.4) 
## 
## Order selected 4  sigma^2 estimated as  4503
## 
## 
## 
## Team:  Cleveland.Cavaliers 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4  
##  0.5780  -0.4117   0.0644  -0.4807  
## 
## Intercept: 1874 (406.5) 
## 
## Order selected 4  sigma^2 estimated as  6269
## 
## 
## 
## Team:  Charlotte.Hornets 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2  
##  0.6590  -0.4131  
## 
## Intercept: 1072 (269.2) 
## 
## Order selected 2  sigma^2 estimated as  3134
## 
## 
## 
## Team:  Miami.Heat 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3  
##  0.4928  -0.0729  -0.4068  
## 
## Intercept: 1530 (361.6) 
## 
## Order selected 3  sigma^2 estimated as  4802
## 
## 
## 
## Team:  Oklahoma.City.Thunder 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##      1  
## 0.6664  
## 
## Intercept: 512.2 (273.8) 
## 
## Order selected 1  sigma^2 estimated as  6454
## 
## 
## 
## Team:  Los.Angeles.Lakers 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3  
##  0.9351   0.2284  -0.5602  
## 
## Intercept: 616.1 (182.3) 
## 
## Order selected 3  sigma^2 estimated as  3015
## 
## 
## 
## Team:  Denver.Nuggets 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5  
##  1.0182  -0.5624   0.0248   0.1845  -0.3073  
## 
## Intercept: 990.7 (304.3) 
## 
## Order selected 5  sigma^2 estimated as  1193
## 
## 
## 
## Team:  Phoenix.Suns 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##      1  
## 0.6631  
## 
## Intercept: 509.2 (258.9) 
## 
## Order selected 1  sigma^2 estimated as  7621
## 
## 
## 
## Team:  Los.Angeles.Clippers 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3  
##  1.2054  -0.5266   0.0100  
## 
## Intercept: 479 (207) 
## 
## Order selected 3  sigma^2 estimated as  2857
## 
## 
## 
## Team:  Washington.Wizards 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5  
##  0.7534  -0.2765   0.0511  -0.2146  -0.2201  
## 
## Intercept: 1301 (406.9) 
## 
## Order selected 5  sigma^2 estimated as  1808
## 
## 
## 
## Team:  Detroit.Pistons 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5        6  
##  0.7734   0.0502  -0.0178  -0.3711   0.2493  -0.0945  
## 
## Intercept: 590.3 (261.5) 
## 
## Order selected 6  sigma^2 estimated as  2173
## 
## 
## 
## Team:  Indiana.Pacers 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4  
##  0.3621   0.1419  -0.2554  -0.2929  
## 
## Intercept: 1580 (404.4) 
## 
## Order selected 4  sigma^2 estimated as  1618
## 
## 
## 
## Team:  Minnesota.Timberwolves 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5        6  
##  0.5536  -0.1773  -0.2925   0.0145   0.4705  -0.5237  
## 
## Intercept: 1350 (335.3) 
## 
## Order selected 6  sigma^2 estimated as  2552
## 
## 
## 
## Team:  Milwaukee.Bucks 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##      1  
## 0.6983  
## 
## Intercept: 449.6 (253.3) 
## 
## Order selected 1  sigma^2 estimated as  4772
## 
## 
## 
## Team:  Sacramento.Kings 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5        6  
##  0.1780   0.0992  -0.0784   0.3816   0.0660  -0.4292  
## 
## Intercept: 1110 (275.7) 
## 
## Order selected 6  sigma^2 estimated as  984
## 
## 
## 
## Team:  New.Orleans.Pelicans 
## 
## 
## Call:
## ar.ols(x = df[, i], order.max = 6, demean = F, intercept = T)
## 
## Coefficients:
##       1        2        3        4        5        6  
##  0.1713  -0.1348  -0.2279  -0.2924   0.0340  -0.3546  
## 
## Intercept: 2665 (723.5) 
## 
## Order selected 6  sigma^2 estimated as  2177

Durbin-Watson test statistic and model

library("tidyverse")
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.1
## ✔ readr   2.1.2     ✔ forcats 0.5.2
## ✔ purrr   0.3.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ dplyr::recode() masks car::recode()
## ✖ purrr::some()   masks car::some()
# create a list to store the prediction for the next year average elo ratings
predict_result=list()

#index for the optimal order list
count=1
# loop to go over each team
for(i in team_name){
  
  # create a list to store the each lag column like lag1, lag2...
  group=list()
  
  # each team's elo ratings dataframe
  current_team=df[i]
  
  # optimal order
  counter<-opt_order[[count]]
 
  # create lag column based on the optimal order
  for(j in 1:counter){
    # create the lag column using dplyr
    current_team<-current_team %>%
    dplyr::mutate("lag{j}":=lag(df[,i],n=j,default=NA))
    
    # construct the lag column name string for later use and store it in the group list
    temp=paste("lag",j,sep = "")
    group=append(group,temp)
  }
  # go to the next optimal order for the next team
  count=count+1

 # construct the model formula to general model
  mymodel<-as.formula(paste(i,paste(group,collapse = "+"),sep = "~"))


 # use model lm function and model formula to generate the model
  current_model=lm(mymodel,data=current_team)
  # formmating
  cat(paste("Team: ",i))

  cat("\n")
  # print the summary of the model
  print(summary(current_model))
  
  # get the prediction for the next year elo ratings 
  prediction=tail(current_team,n=1)

  cat("The prediction for 2021 is the following\n")
  # output the predicted next year elo ratings
  print(predict(current_model,prediction))
  # store the predicted result into list
  predict_result=append(predict_result,predict(current_model,prediction))

  cat("\n")
  # run the durbinwatsonTest
  print(durbinWatsonTest(current_model))

cat("\n")
cat("\n")
}
## Team:  Orlando.Magic
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -129.883  -43.798    9.296   52.084   92.512 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 548.2074   244.1129   2.246 0.039200 *  
## lag1          1.0700     0.2332   4.588 0.000303 ***
## lag2         -0.4469     0.2324  -1.922 0.072533 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 72.86 on 16 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.6207, Adjusted R-squared:  0.5733 
## F-statistic: 13.09 on 2 and 16 DF,  p-value: 0.000428
## 
## The prediction for 2021 is the following
##      21 
## 1493.33 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.08751531      2.054502   0.814
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Atlanta.Hawks
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -80.55 -38.04 -16.41  40.38 146.03 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 702.552484 358.429862   1.960   0.0784 .
## lag1          0.829292   0.279104   2.971   0.0140 *
## lag2         -0.002759   0.383349  -0.007   0.9944  
## lag3         -0.462419   0.374308  -1.235   0.2449  
## lag4         -0.037202   0.403784  -0.092   0.9284  
## lag5          0.204818   0.297235   0.689   0.5064  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 70.18 on 10 degrees of freedom
##   (5 observations deleted due to missingness)
## Multiple R-squared:  0.6595, Adjusted R-squared:  0.4893 
## F-statistic: 3.874 on 5 and 10 DF,  p-value: 0.03258
## 
## The prediction for 2021 is the following
##       21 
## 1449.385 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      -0.1165448      2.135864    0.87
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Brooklyn.Nets
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -60.090 -41.876  -9.217  32.919 111.090 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 1220.67615  526.67341   2.318   0.0491 *
## lag1           0.64744    0.27456   2.358   0.0461 *
## lag2          -0.01073    0.31975  -0.034   0.9741  
## lag3          -0.53591    0.31813  -1.685   0.1306  
## lag4          -0.21001    0.30762  -0.683   0.5141  
## lag5           0.47629    0.32086   1.484   0.1760  
## lag6          -0.22630    0.25485  -0.888   0.4005  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 68.52 on 8 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.649,  Adjusted R-squared:  0.3857 
## F-statistic: 2.465 on 6 and 8 DF,  p-value: 0.1185
## 
## The prediction for 2021 is the following
##       21 
## 1467.772 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      -0.3890731      2.358337   0.886
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Toronto.Raptors
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -109.33  -30.75   12.38   41.02   83.08 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 521.32031  286.07583   1.822  0.08983 . 
## lag1          0.87755    0.25440   3.450  0.00391 **
## lag2         -0.02344    0.35904  -0.065  0.94888   
## lag3         -0.19932    0.28243  -0.706  0.49193   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 66.49 on 14 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.6588, Adjusted R-squared:  0.5857 
## F-statistic: 9.011 on 3 and 14 DF,  p-value: 0.001416
## 
## The prediction for 2021 is the following
##       21 
## 1628.819 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.01541888      1.868053   0.574
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  New.York.Knicks
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -107.200  -33.972    0.043   16.756  150.296 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 904.2492   287.8373   3.142  0.00564 **
## lag1          0.3608     0.2024   1.782  0.09156 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 65.24 on 18 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:   0.15,  Adjusted R-squared:  0.1028 
## F-statistic: 3.177 on 1 and 18 DF,  p-value: 0.09156
## 
## The prediction for 2021 is the following
##       21 
## 1377.629 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1       0.1376077      1.569834   0.228
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Dallas.Mavericks
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -52.839 -24.192  -5.515  14.407  83.610 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  85.29450  404.03740   0.211  0.83704   
## lag1          1.07295    0.26939   3.983  0.00259 **
## lag2         -0.88049    0.39620  -2.222  0.05050 . 
## lag3          0.49677    0.41936   1.185  0.26356   
## lag4          0.26618    0.37030   0.719  0.48870   
## lag5         -0.02313    0.38972  -0.059  0.95385   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 50.09 on 10 degrees of freedom
##   (5 observations deleted due to missingness)
## Multiple R-squared:  0.7866, Adjusted R-squared:  0.6798 
## F-statistic:  7.37 on 5 and 10 DF,  p-value: 0.003875
## 
## The prediction for 2021 is the following
##       21 
## 1537.109 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      -0.1438985      2.246477   0.946
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Houston.Rockets
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -66.470 -40.707   8.202  35.983 112.950 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 2874.86313 1016.48613   2.828   0.0222 *
## lag1           0.38293    0.38574   0.993   0.3499  
## lag2          -0.39509    0.46644  -0.847   0.4216  
## lag3          -0.08973    0.49966  -0.180   0.8619  
## lag4          -0.68869    0.59113  -1.165   0.2776  
## lag5           0.74495    0.48896   1.524   0.1661  
## lag6          -0.78395    0.42491  -1.845   0.1023  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 64.99 on 8 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.5266, Adjusted R-squared:  0.1716 
## F-statistic: 1.483 on 6 and 8 DF,  p-value: 0.2953
## 
## The prediction for 2021 is the following
##       21 
## 1453.666 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      0.05963411      1.744307   0.382
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  San.Antonio.Spurs
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -62.496 -25.699  -3.759  12.931  77.660 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 1897.89288  747.43059   2.539  0.02598 * 
## lag1           0.87953    0.24733   3.556  0.00395 **
## lag2          -0.29057    0.35733  -0.813  0.43196   
## lag3           0.08296    0.35409   0.234  0.81871   
## lag4          -0.81052    0.37615  -2.155  0.05219 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 43.9 on 12 degrees of freedom
##   (4 observations deleted due to missingness)
## Multiple R-squared:  0.7504, Adjusted R-squared:  0.6671 
## F-statistic: 9.017 on 4 and 12 DF,  p-value: 0.001332
## 
## The prediction for 2021 is the following
##       21 
## 1510.932 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1     0.008788928      1.906495   0.704
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Chicago.Bulls
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -111.855  -38.155   -7.505   34.493  139.689 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 415.3962   188.6589   2.202    0.041 *  
## lag1          0.7219     0.1287   5.610 2.53e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 64.73 on 18 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.6361, Adjusted R-squared:  0.6159 
## F-statistic: 31.47 on 1 and 18 DF,  p-value: 2.531e-05
## 
## The prediction for 2021 is the following
##       21 
## 1397.138 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1       0.1782521       1.61567   0.296
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Utah.Jazz
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -131.757  -17.475    2.464   28.021   82.945 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 774.3439   326.7613   2.370  0.03071 * 
## lag1          0.9136     0.2379   3.840  0.00144 **
## lag2         -0.4152     0.2325  -1.785  0.09315 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 54.42 on 16 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.4857, Adjusted R-squared:  0.4214 
## F-statistic: 7.554 on 2 and 16 DF,  p-value: 0.004899
## 
## The prediction for 2021 is the following
##      21 
## 1567.61 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.02094699      1.885016   0.638
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Portland.Trailblazers
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -79.838 -23.193   6.735  28.045  91.984 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 1024.37752  693.43988   1.477   0.1779  
## lag1           0.69707    0.30862   2.259   0.0538 .
## lag2          -0.44227    0.40872  -1.082   0.3108  
## lag3           0.16894    0.47024   0.359   0.7287  
## lag4          -0.10474    0.43105  -0.243   0.8141  
## lag5           0.06779    0.38650   0.175   0.8651  
## lag6          -0.05305    0.27819  -0.191   0.8535  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 60.57 on 8 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.4691, Adjusted R-squared:  0.07094 
## F-statistic: 1.178 on 6 and 8 DF,  p-value: 0.4033
## 
## The prediction for 2021 is the following
##       21 
## 1489.709 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      -0.2811096      2.404132   0.884
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Golden.State.Warriors
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -228.266  -17.649    7.724   41.909  140.660 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 372.2937   205.1273   1.815   0.0862 .  
## lag1          0.7605     0.1341   5.672 2.22e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 87.78 on 18 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.6412, Adjusted R-squared:  0.6213 
## F-statistic: 32.17 on 1 and 18 DF,  p-value: 2.221e-05
## 
## The prediction for 2021 is the following
##       21 
## 1452.333 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1       0.2049787      1.571606   0.222
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Memphis.Grizzlies
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -51.046 -30.535  -2.213  28.174  81.629 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 860.0667   430.2933   1.999  0.08067 . 
## lag1          0.9139     0.2815   3.246  0.01176 * 
## lag2          0.4658     0.3783   1.231  0.25321   
## lag3         -1.3497     0.3390  -3.982  0.00405 **
## lag4          0.2278     0.3471   0.656  0.53004   
## lag5          0.6656     0.3564   1.868  0.09878 . 
## lag6         -0.4960     0.2537  -1.955  0.08626 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 50.24 on 8 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.8441, Adjusted R-squared:  0.7271 
## F-statistic: 7.217 on 6 and 8 DF,  p-value: 0.006801
## 
## The prediction for 2021 is the following
##       21 
## 1509.908 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      0.05160347      1.752942   0.272
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Philadelphia.76ers
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -111.153  -24.370   -3.418   32.528   73.506 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 942.4675   394.9845   2.386 0.038213 *  
## lag1          1.2578     0.2356   5.340 0.000328 ***
## lag2         -1.1063     0.3330  -3.323 0.007713 ** 
## lag3          0.8756     0.3471   2.523 0.030255 *  
## lag4         -1.1389     0.3433  -3.317 0.007781 ** 
## lag5          0.4537     0.2501   1.814 0.099724 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 54.95 on 10 degrees of freedom
##   (5 observations deleted due to missingness)
## Multiple R-squared:  0.8515, Adjusted R-squared:  0.7772 
## F-statistic: 11.47 on 5 and 10 DF,  p-value: 0.0006953
## 
## The prediction for 2021 is the following
##       21 
## 1563.759 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1       0.1419326      1.661627   0.248
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Boston.Celtics
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -130.887  -47.102    6.087   42.326  139.743 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 1628.8647   493.2323   3.302  0.00631 **
## lag1           0.5609     0.2378   2.358  0.03616 * 
## lag2          -0.2759     0.2879  -0.959  0.35672   
## lag3           0.1522     0.2856   0.533  0.60377   
## lag4          -0.4906     0.2413  -2.033  0.06480 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 79.87 on 12 degrees of freedom
##   (4 observations deleted due to missingness)
## Multiple R-squared:  0.4922, Adjusted R-squared:  0.3229 
## F-statistic: 2.908 on 4 and 12 DF,  p-value: 0.06778
## 
## The prediction for 2021 is the following
##      21 
## 1579.93 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      -0.1064543      2.171657   0.958
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Cleveland.Cavaliers
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -138.219  -43.604    4.571   27.044  203.068 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 1873.53219  483.78968   3.873  0.00222 **
## lag1           0.57797    0.25270   2.287  0.04114 * 
## lag2          -0.41174    0.29352  -1.403  0.18602   
## lag3           0.06439    0.30560   0.211  0.83665   
## lag4          -0.48065    0.25318  -1.898  0.08194 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 94.24 on 12 degrees of freedom
##   (4 observations deleted due to missingness)
## Multiple R-squared:  0.6523, Adjusted R-squared:  0.5364 
## F-statistic: 5.629 on 4 and 12 DF,  p-value: 0.008678
## 
## The prediction for 2021 is the following
##      21 
## 1387.29 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.01576869      2.007691   0.848
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Charlotte.Hornets
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -127.020  -44.501    4.702   55.194   73.284 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 1072.0092   293.3040   3.655  0.00214 **
## lag1           0.6590     0.2224   2.963  0.00916 **
## lag2          -0.4131     0.2017  -2.048  0.05739 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 61.01 on 16 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared:  0.3621, Adjusted R-squared:  0.2823 
## F-statistic: 4.541 on 2 and 16 DF,  p-value: 0.02743
## 
## The prediction for 2021 is the following
##       21 
## 1383.525 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.02894974      1.962441   0.758
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Miami.Heat
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -165.01  -43.99    1.86   41.10   96.33 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 1529.98383  410.00792   3.732  0.00223 **
## lag1           0.49283    0.24421   2.018  0.06317 . 
## lag2          -0.07289    0.28220  -0.258  0.79994   
## lag3          -0.40684    0.23953  -1.699  0.11152   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 78.57 on 14 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.4874, Adjusted R-squared:  0.3775 
## F-statistic: 4.437 on 3 and 14 DF,  p-value: 0.02168
## 
## The prediction for 2021 is the following
##      21 
## 1582.32 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      0.09993848      1.768211   0.468
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Oklahoma.City.Thunder
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -140.701  -43.292   -6.108   68.452  147.915 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 512.1811   288.6448   1.774  0.09291 . 
## lag1          0.6664     0.1859   3.584  0.00212 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 84.68 on 18 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.4164, Adjusted R-squared:  0.384 
## F-statistic: 12.85 on 1 and 18 DF,  p-value: 0.002121
## 
## The prediction for 2021 is the following
##       21 
## 1559.582 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1       0.1132656      1.619931    0.27
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Los.Angeles.Lakers
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -69.737 -49.630  -5.164  46.180  91.914 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 616.0668   206.7589   2.980 0.009945 ** 
## lag1          0.9351     0.2244   4.168 0.000949 ***
## lag2          0.2284     0.3485   0.656 0.522737    
## lag3         -0.5602     0.2405  -2.330 0.035300 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 62.26 on 14 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.8115, Adjusted R-squared:  0.7711 
## F-statistic: 20.09 on 3 and 14 DF,  p-value: 2.427e-05
## 
## The prediction for 2021 is the following
##       21 
## 1689.464 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      0.08234792      1.690125   0.322
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Denver.Nuggets
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -90.587 -17.687  -4.533  23.024  67.624 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 990.68086  384.88584   2.574  0.02770 * 
## lag1          1.01817    0.27546   3.696  0.00413 **
## lag2         -0.56239    0.36279  -1.550  0.15214   
## lag3          0.02476    0.28639   0.086  0.93281   
## lag4          0.18452    0.26319   0.701  0.49923   
## lag5         -0.30728    0.19439  -1.581  0.14502   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 43.69 on 10 degrees of freedom
##   (5 observations deleted due to missingness)
## Multiple R-squared:  0.6956, Adjusted R-squared:  0.5434 
## F-statistic:  4.57 on 5 and 10 DF,  p-value: 0.0198
## 
## The prediction for 2021 is the following
##       21 
## 1589.593 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      -0.1386837      2.249466    0.91
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Phoenix.Suns
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -151.253  -77.410   -8.986   55.595  183.052 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 509.1778   272.8555   1.866  0.07841 . 
## lag1          0.6631     0.1806   3.672  0.00175 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 92.02 on 18 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.4282, Adjusted R-squared:  0.3964 
## F-statistic: 13.48 on 1 and 18 DF,  p-value: 0.001746
## 
## The prediction for 2021 is the following
##       21 
## 1445.768 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1       0.1435297      1.477901   0.156
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Los.Angeles.Clippers
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -88.811 -31.860   5.439  30.154 102.673 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 479.010068 234.760550   2.040   0.0606 .  
## lag1          1.205356   0.223344   5.397 9.41e-05 ***
## lag2         -0.526619   0.306094  -1.720   0.1074    
## lag3          0.009993   0.205811   0.049   0.9620    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 60.61 on 14 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.7487, Adjusted R-squared:  0.6948 
## F-statistic:  13.9 on 3 and 14 DF,  p-value: 0.0001757
## 
## The prediction for 2021 is the following
##       21 
## 1635.527 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      0.01651286      1.966173    0.62
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Washington.Wizards
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -108.285  -16.176    7.182   37.253   54.742 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 1301.36338  514.70081   2.528   0.0300 *
## lag1           0.75344    0.29876   2.522   0.0303 *
## lag2          -0.27651    0.31767  -0.870   0.4045  
## lag3           0.05106    0.29459   0.173   0.8658  
## lag4          -0.21457    0.29515  -0.727   0.4839  
## lag5          -0.22006    0.27454  -0.802   0.4414  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 53.79 on 10 degrees of freedom
##   (5 observations deleted due to missingness)
## Multiple R-squared:  0.7181, Adjusted R-squared:  0.5772 
## F-statistic: 5.095 on 5 and 10 DF,  p-value: 0.01398
## 
## The prediction for 2021 is the following
##       21 
## 1371.575 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.05418594      2.046179   0.818
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Detroit.Pistons
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -64.249 -31.104  -5.458   6.953 103.234 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 590.26365  358.09516   1.648   0.1379  
## lag1          0.77344    0.29164   2.652   0.0292 *
## lag2          0.05017    0.37095   0.135   0.8958  
## lag3         -0.01783    0.36535  -0.049   0.9623  
## lag4         -0.37110    0.36455  -1.018   0.3385  
## lag5          0.24931    0.37848   0.659   0.5286  
## lag6         -0.09453    0.28305  -0.334   0.7470  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 63.83 on 8 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.7314, Adjusted R-squared:   0.53 
## F-statistic: 3.631 on 6 and 8 DF,  p-value: 0.04826
## 
## The prediction for 2021 is the following
##       21 
## 1420.803 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.01487294      1.922345   0.506
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Indiana.Pacers
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -89.603 -28.607  -1.564  36.577  54.630 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 1579.6909   481.3800   3.282  0.00656 **
## lag1           0.3621     0.2445   1.481  0.16434   
## lag2           0.1419     0.2616   0.542  0.59748   
## lag3          -0.2554     0.2603  -0.981  0.34598   
## lag4          -0.2929     0.2442  -1.199  0.25352   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 47.88 on 12 degrees of freedom
##   (4 observations deleted due to missingness)
## Multiple R-squared:  0.4788, Adjusted R-squared:  0.3051 
## F-statistic: 2.756 on 4 and 12 DF,  p-value: 0.07764
## 
## The prediction for 2021 is the following
##      21 
## 1536.01 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      0.07777077      1.801042   0.548
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Minnesota.Timberwolves
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -98.648 -38.581  -9.233  52.240  68.776 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 1350.3939   459.1307   2.941   0.0187 *
## lag1           0.5536     0.2747   2.015   0.0787 .
## lag2          -0.1773     0.3095  -0.573   0.5824  
## lag3          -0.2925     0.2918  -1.002   0.3456  
## lag4           0.0145     0.3012   0.048   0.9628  
## lag5           0.4705     0.2902   1.621   0.1436  
## lag6          -0.5237     0.2462  -2.127   0.0661 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 69.18 on 8 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.5645, Adjusted R-squared:  0.2379 
## F-statistic: 1.728 on 6 and 8 DF,  p-value: 0.2318
## 
## The prediction for 2021 is the following
##       21 
## 1363.549 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      -0.2611788      2.388298   0.716
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Milwaukee.Bucks
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -178.289  -30.933   -4.362   27.501  158.328 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 449.6498   266.9548   1.684  0.10937   
## lag1          0.6983     0.1797   3.885  0.00108 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 72.82 on 18 degrees of freedom
##   (1 observation deleted due to missingness)
## Multiple R-squared:  0.4561, Adjusted R-squared:  0.4259 
## F-statistic: 15.09 on 1 and 18 DF,  p-value: 0.001085
## 
## The prediction for 2021 is the following
##       21 
## 1646.317 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1       0.0415015      1.910716   0.692
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  Sacramento.Kings
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -72.88 -17.98  11.15  23.91  38.35 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 1109.64864  377.45616   2.940   0.0187 *
## lag1           0.17796    0.32465   0.548   0.5985  
## lag2           0.09916    0.31510   0.315   0.7610  
## lag3          -0.07840    0.34851  -0.225   0.8276  
## lag4           0.38162    0.45695   0.835   0.4279  
## lag5           0.06601    0.46659   0.141   0.8910  
## lag6          -0.42918    0.32963  -1.302   0.2291  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 42.95 on 8 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.5545, Adjusted R-squared:  0.2204 
## F-statistic:  1.66 on 6 and 8 DF,  p-value: 0.2478
## 
## The prediction for 2021 is the following
##       21 
## 1434.024 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1      0.06412816      1.824452   0.458
##  Alternative hypothesis: rho != 0
## 
## 
## Team:  New.Orleans.Pelicans
## 
## Call:
## lm(formula = mymodel, data = current_team)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -75.500 -34.169  -2.281  37.381  80.654 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 2664.89637  990.70214   2.690   0.0275 *
## lag1           0.17128    0.31676   0.541   0.6034  
## lag2          -0.13482    0.27207  -0.496   0.6336  
## lag3          -0.22786    0.26847  -0.849   0.4207  
## lag4          -0.29237    0.28088  -1.041   0.3284  
## lag5           0.03403    0.27875   0.122   0.9059  
## lag6          -0.35459    0.25807  -1.374   0.2067  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 63.89 on 8 degrees of freedom
##   (6 observations deleted due to missingness)
## Multiple R-squared:  0.3871, Adjusted R-squared:  -0.07252 
## F-statistic: 0.8422 on 6 and 8 DF,  p-value: 0.571
## 
## The prediction for 2021 is the following
##       21 
## 1466.278 
## 
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.05424297      2.042947   0.906
##  Alternative hypothesis: rho != 0

Get the prediction

# convert list to matrix
predict_result=do.call(rbind, predict_result)
# rename column name
colnames(predict_result)="Predict"
# add team name
predict_result=cbind(team_name,predict_result)

# convert matrix to dataframe
predict_result=as.data.frame(predict_result)
# remove row name
predict_result = predict_result %>% `rownames<-`( NULL )
predict_result
##                 team_name          Predict
## 1           Orlando.Magic 1493.33004993858
## 2           Atlanta.Hawks 1449.38457393284
## 3           Brooklyn.Nets 1467.77212811047
## 4         Toronto.Raptors 1628.81857631474
## 5         New.York.Knicks 1377.62923114113
## 6        Dallas.Mavericks 1537.10852982091
## 7         Houston.Rockets 1453.66626037168
## 8       San.Antonio.Spurs 1510.93176491456
## 9           Chicago.Bulls  1397.1383544785
## 10              Utah.Jazz 1567.61004512357
## 11  Portland.Trailblazers 1489.70943810751
## 12  Golden.State.Warriors 1452.33330039421
## 13      Memphis.Grizzlies 1509.90770311615
## 14     Philadelphia.76ers 1563.75915498451
## 15         Boston.Celtics 1579.93023353183
## 16    Cleveland.Cavaliers 1387.28971454438
## 17      Charlotte.Hornets 1383.52479413362
## 18             Miami.Heat 1582.32004424647
## 19  Oklahoma.City.Thunder 1559.58239450994
## 20     Los.Angeles.Lakers 1689.46399915897
## 21         Denver.Nuggets 1589.59252061288
## 22           Phoenix.Suns 1445.76839336782
## 23   Los.Angeles.Clippers 1635.52743466796
## 24     Washington.Wizards 1371.57539415334
## 25        Detroit.Pistons 1420.80301072447
## 26         Indiana.Pacers 1536.00965953525
## 27 Minnesota.Timberwolves 1363.54929904922
## 28        Milwaukee.Bucks 1646.31737219909
## 29       Sacramento.Kings 1434.02384036557
## 30   New.Orleans.Pelicans 1466.27840858028